package org.crawler.processor;

import java.util.Map;

import org.crawler.task.AbstractTaskQueue;
import org.crawler.task.FreshUrlMap;
import org.crawler.task.TaskCenter;
import org.crawler.taskobj.WebUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class WebUrlCreater extends Thread implements  IProcess{
	
	@Autowired 
	private TaskCenter taskCenter;

	@Override
	public void innerProcess() {
		
		FreshUrlMap task=taskCenter.getFreshUrlMap();
		AbstractTaskQueue out=taskCenter.getWebUrl();
		// TODO Auto-generated method stub
		//System.out.println("[== WebUrlCreater ==] begin to process...");
		//System.out.println(task.getTaskCount());
		WebUrl temp=null;
		
		 for (Map.Entry<String, WebUrl> entry : task.getFreshUrlMap().entrySet()) {
			   //System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
			   temp=(WebUrl) task.getFreshUrlMap().remove(entry.getKey());
			   if((!(temp.getUrl().indexOf(temp.getHost())>=0))   && (!temp.getUrl().startsWith("http")))
			   {
			       temp.setUrl(temp.getHost()+temp.getUrl());
			   }
			   out.add(temp);
		}
		// task.getFreshUrlMap().clear();

		//System.out.println("[== WebUrlCreater ==] Completed");
	}
	
	
	public void run()
	{
		while(true)
		{
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			innerProcess();
		}
	}
	
	


}
